package com.gzdh.dishrecognition.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.util.KeyAdd;

public class NumberKeyView extends LinearLayout {

    private Context mContext;

    private TextView b1;
    private TextView b2;
    private TextView b3;
    private TextView b4;
    private TextView b5;
    private TextView b6;
    private TextView b7;
    private TextView b8;
    private TextView b9;
    private TextView b0;
    private TextView bd;
    private TextView bdel;

    private String str_cash = "0";

    private NumberKeyListener numberKeyListener;

    public void clear() {
        str_cash = "0";
        if (numberKeyListener != null) {
            numberKeyListener.onClick(str_cash, "");
        }
    }

    public void reloadDisplaySize(){
        int size = SysConstConfig.getInstance().getRightKeyboardDisplaySize();
        int height;
        int fontSize;
        switch (size) {
            case 0:
                fontSize = 16;
                height = 40;
                break;
            case 1:
                fontSize = 24;
                height = 60;
                break;
            default:
                fontSize = 30;
                height = 70;
                break;
        }
        int heightInPx = (int)(height * getResources().getDisplayMetrics().density);
        TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, heightInPx);
        params.setMargins(1, 1, 1, 1);
        params.weight = 1;
        b1.setLayoutParams(params);
        b2.setLayoutParams(params);
        b3.setLayoutParams(params);
        b4.setLayoutParams(params);
        b5.setLayoutParams(params);
        b6.setLayoutParams(params);
        b7.setLayoutParams(params);
        b8.setLayoutParams(params);
        b9.setLayoutParams(params);
        b0.setLayoutParams(params);
        bd.setLayoutParams(params);
        bdel.setLayoutParams(params);
        b1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b3.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b4.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b5.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b6.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b7.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b8.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b9.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        b0.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        bd.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        bdel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
    }

    public void setVal(String cash){
        this.str_cash = cash;
    }

    public void setNumberKeyListener(NumberKeyListener numberKeyListener) {
        this.numberKeyListener = numberKeyListener;
    }

    public NumberKeyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_number_key, this, true);
        mContext = context;
        initView();
        initDate();
    }

    private void initView() {
        b1 = findViewById(R.id.b1);
        b2 = findViewById(R.id.b2);
        b3 = findViewById(R.id.b3);
        b4 = findViewById(R.id.b4);
        b5 = findViewById(R.id.b5);
        b6 = findViewById(R.id.b6);
        b7 = findViewById(R.id.b7);
        b8 = findViewById(R.id.b8);
        b9 = findViewById(R.id.b9);
        b0 = findViewById(R.id.b0);
        bd = findViewById(R.id.bd);
        bdel = findViewById(R.id.bdel);
    }

    private void initDate() {
        b1.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "1");

            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "1");
            }
        });
        b2.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "2");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "2");
            }
        });
        b3.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "3");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "3");
            }
        });
        b4.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "4");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "4");
            }
        });
        b5.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "5");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "5");
            }
        });
        b6.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "6");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "6");
            }
        });
        b7.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "7");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "7");
            }
        });
        b8.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "8");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "8");
            }
        });
        b9.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "9");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "9");
            }
        });
        b0.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "0");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "0");
            }
        });
        bd.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, ".");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, ".");
            }
        });
        bdel.setOnClickListener((v) -> {
            str_cash = KeyAdd.getKeyNum(str_cash, "c");
            if (numberKeyListener != null) {
                numberKeyListener.onClick(str_cash, "c");
            }
        });
    }

    public interface NumberKeyListener {
        void onClick(String cashStr, String key);
    }

}
